package lc;

public class Ex152 {
    class Solution {
        public int maxProduct(int[] nums) {
            int n = nums.length, res = Integer.MIN_VALUE;
            int[] max = new int[n];
            int[] min = new int[n];
            max[0] = min[0] = nums[0];
            for (int i = 0; i < n; i++) {
               if (nums[i] >= 0) {
                    max[i] = Math.max(max[i - 1] * nums[i], nums[i]);
                    min[i] = Math.min(min[i - 1] * nums[i], nums[i]);
               } else {
                    max[i] = Math.max(min[i - 1] * nums[i], nums[i]);
                    min[i] = Math.min(max[i - 1] * nums[i], nums[i]);
               }
               res = Math.max(max[i], res);
            }
            return res;
        }
    }
}
